package com.tmc.web.controller.intair;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.tmc.bean.common.CommonResult;
import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.intair.EthBookingReq;
import com.tmc.bean.intair.EthFare;
import com.tmc.bean.intair.EthOrder;
import com.tmc.bean.intair.IntairShoppingReq;
import com.tmc.service.intair.IntairCoreServiceImpl;
import com.tmc.util.IntairFareUtil;
import com.tmc.util.QrCodeUtil;
import com.tmc.web.BaseController;

@Controller
@RequestMapping("/intair")
public class IntairController extends BaseController {

	@Autowired
	private IntairCoreServiceImpl intairSer;

	@RequestMapping(value = "/i-search-page")
	public ModelAndView excute(HttpServletRequest request, IntairShoppingReq req) {
		ContextInfo context = this.getContextInfo(request);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("context", context);
		map.put("req", req);
		map.put("config", intairSer.getShoppingCfg(context));
		return new ModelAndView("views/intair", map);
	}

	@RequestMapping(value = "/i-shopping-cfg", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String IshoppingCfg(HttpServletRequest request) {
		ContextInfo context = this.getContextInfo(request);
		String config = intairSer.getShoppingCfg(context);
		return CommonResult.makeSuccess(config).toJson();
	}

	@RequestMapping(value = "/i-shopping", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String Ishopping(HttpServletRequest request, IntairShoppingReq req) {
		ContextInfo context = this.getContextInfo(request);
		List<EthFare> fares = intairSer.shopping(context, req);
		return JSON.toJSONString(fares);
	}

	@RequestMapping(value = "/i-pricing", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String Ipricing(HttpServletRequest request, String fareKey) {
		ContextInfo context = this.getContextInfo(request);
		if (context == null) {
			return CommonResult.makeFail("未登陆或token过期！101").toJson();
		}
		CommonResult cr = intairSer.pricing(context, fareKey);
		return cr.toJson();
	}

	@RequestMapping(value = "/i-booking-page", produces = "text/html;charset=UTF-8")
	public ModelAndView IBookingPage(HttpServletRequest request) {
		ContextInfo context = this.getContextInfo(request);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("context", context);
		map.put("fare", intairSer.getPricingFare(context));
		return new ModelAndView("views/ibooking", map);
	}

	@RequestMapping(value = "/i-booking", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String IBooking(HttpServletRequest request, EthBookingReq req) {
		ContextInfo context = this.getContextInfo(request);
		if (context == null) {
			return CommonResult.makeFail("未登陆或token过期！").toJson();
		}
		CommonResult cr = intairSer.booking(context, req);
		return cr.toJson();
	}

	@RequestMapping(value = "/i-checking-page")
	public ModelAndView ICheckingPage(HttpServletRequest request) {
		ContextInfo context = this.getContextInfo(request);
		Map<String, Object> map = new HashMap<String, Object>();
		EthOrder order = intairSer.getCurrentOrder(context);
		map.put("context", context);
		map.put("order", order);
		EthFare fare = order.getPassengers().get(0).getFare();
		fare.setFlights(order.getFlights());
		IntairFareUtil.setExtInfo(fare);
		map.put("fare", fare);
		return new ModelAndView("views/ichecking", map);
	}
	
	@RequestMapping(value = "/i-order-page")
	public ModelAndView IOrderPage(HttpServletRequest request) {
		ContextInfo context = this.getContextInfo(request);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("context", context);
		map.put("page", intairSer.findMyOrders(context));
		return new ModelAndView("views/order_intair", map);
	}
	
	@RequestMapping(value = "/wxpayqrcode")
	public void wxpayqrcode(HttpServletRequest request, HttpServletResponse response, String random) throws IOException {
		ContextInfo context = this.getContextInfo(request);
		// 禁止图像缓存。
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/jpeg");
		// 将图像输出到Servlet输出流中。
		ServletOutputStream sos = response.getOutputStream();
		EthOrder order = intairSer.getCurrentOrder(context);
		try {
			BufferedImage img = QrCodeUtil.createWXPay(order.getOrderNum(), order.getTotalAmount());
			ImageIO.write(img, "png", sos);
		} catch (Exception e) {
			
		}
		sos.close();
	}
	
	@RequestMapping(value = "/alipayqrcode")
	public void alipayqrcode(HttpServletRequest request, HttpServletResponse response, String random) throws IOException {
		ContextInfo context = this.getContextInfo(request);
		// 禁止图像缓存。
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/jpeg");
		// 将图像输出到Servlet输出流中。
		ServletOutputStream sos = response.getOutputStream();
		EthOrder order = intairSer.getCurrentOrder(context);
		try {
			BufferedImage img = QrCodeUtil.createAliPay(order.getOrderNum(), order.getTotalAmount());
			ImageIO.write(img, "png", sos);
		} catch (Exception e) {
			
		}
		sos.close();
	}
	
}
